//
//  TIMFriendDetailVC.m
//  Demo_sdk
//
//  Created by Deli Xia on 2018/7/7.
//  Copyright © 2018年 Deli Xia. All rights reserved.
//

#import "TIMFriendDetailVC.h"
#import "TIMFriendInfoCell.h"
#import "TIMDemoMacro.h"
#import "Masonry.h"
#import "UIView+Extension.h"

#import "TIMStorageModel.h"
#import "TIMStorageManager.h"
#import "TIMApi.h"

@interface TIMFriendDetailVC ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic,strong) UITableView *friendInfoTab;//!<

@property (nonatomic, strong) TIMUserInfo *friendInfo;

@end

@implementation TIMFriendDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = DefaultBackGroundColor;
    [self.view addSubview:self.friendInfoTab];
}

#pragma mark - UITableViewDelegate/

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 2;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 52;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 100;
    }
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (section == 1) {
        return 100;
    }
    return 10;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    //viewforHeader
    if (section == 0) {
        static NSString * identy = @"head";
        UITableViewHeaderFooterView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:identy];
        if (!headerView) {
            NSLog(@"%li",section);
            headerView = [[UITableViewHeaderFooterView alloc]initWithReuseIdentifier:identy];
            UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 10, Screen_Width, 80)];
            view.backgroundColor = [UIColor whiteColor];
            [headerView.contentView addSubview:view];
            
            UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(15, 20, 60, 60)];
            imgView.layer.cornerRadius = imgView.height/2;
            imgView.layer.masksToBounds = YES;
            [headerView.contentView addSubview:imgView];
            imgView.userInteractionEnabled = YES;
            imgView.tag = 300;
            
            UITapGestureRecognizer *TapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imgViewClicked)];
            [imgView addGestureRecognizer:TapGes];
            
            UILabel *remarksName = [[UILabel alloc] initWithFrame:CGRectMake(90, 20, Screen_Width - 100, 20)];
            remarksName.font = [UIFont boldSystemFontOfSize:16];
            remarksName.textColor = RGBColor(51, 51, 51);
            [headerView.contentView addSubview:remarksName];
            
            UILabel *saleRankLabel = [[UILabel alloc] initWithFrame:CGRectMake(90, 40, Screen_Width - 100, 20)];
            saleRankLabel.font = TPSFontSize(12);
            saleRankLabel.textColor = RGBColor(117, 117, 117);
            [headerView.contentView addSubview:saleRankLabel];
            
            UILabel *nickName = [[UILabel alloc] initWithFrame:CGRectMake(90, 60, Screen_Width - 100, 20)];
            nickName.font = TPSFontSize(12);
            nickName.textColor = RGBColor(117, 117, 117);
            [headerView.contentView addSubview:nickName];
            
            remarksName.tag = 100;
            nickName.tag = 200;
            saleRankLabel.tag = 400;
        }
        
        UILabel *remarksName = (UILabel *)[headerView.contentView viewWithTag:100];
        UILabel *nickName = (UILabel *)[headerView.contentView viewWithTag:200];
        UIImageView *imgView = (UIImageView *)[headerView.contentView viewWithTag:300];
        UILabel *saleRankLabel = (UILabel *)[headerView.contentView viewWithTag:400];
        
//        [imgView sd_setImageWithURL:[self.friendModel.headurl filterChineseAndThin] placeholderImage:[UIImage imageNamed:@"userNorm_image"]];
        [imgView setImage:[UIImage imageNamed:@"userNorm_image"]];
        
        if (self.friendInfo.remarkName.length > 0) {
            nickName.text = [NSString stringWithFormat:@"昵称：%@",self.friendInfo.nikeName];
            remarksName.text = self.friendInfo.remarkName;
        } else {
            nickName.text = self.friendInfo.nikeName;
            remarksName.text = self.friendInfo.nikeName;
        }
        if (self.friendInfo.level.length > 0) {
            saleRankLabel.text = [NSString stringWithFormat:@"职称等级：LV%ld", [self.friendInfo.level integerValue] + 1];
        } else {
            saleRankLabel.text = [NSString stringWithFormat:@"职称等级：LV%@", @"1"];
        }
        return headerView;
    }
    return nil;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    static NSString * identy = @"Foot";
    if (section == 1) {
        UITableViewHeaderFooterView *footerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:identy];
        if (!footerView) {
            footerView = [[UITableViewHeaderFooterView alloc]initWithReuseIdentifier:identy];
            UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(15, 20, Screen_Width - 30, 44)];
            [btn setTitle:@"删除好友" forState:UIControlStateNormal];
            [btn addTarget:self action:@selector(deletedClicked) forControlEvents:UIControlEventTouchUpInside];
            btn.titleLabel.font = [UIFont boldSystemFontOfSize:16];
            [btn setTitleColor:UIColor.blackColor forState:UIControlStateNormal];
            [btn setBackgroundColor:UIColor.redColor];
            btn.layer.cornerRadius = 5;
            btn.layer.masksToBounds = YES;
            [footerView addSubview:btn];
        }
        return footerView;
    } else {
        UIView *footer = [[UIView alloc] initWithFrame:Frame(0, 0, Screen_Width, 10)];
        footer.backgroundColor = RGBColor(240, 240, 240);
        return footer;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        TIMFriendInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:[TIMFriendInfoCell description] forIndexPath:indexPath];
        if (indexPath.row == 0) {
            cell.titleLab.text = @"个人简介";
            if (self.friendInfo.persionalnote.length > 0) {
                cell.detaileLab.textAlignment = NSTextAlignmentRight;
                cell.detaileLab.text = self.friendInfo.persionalnote;
            } else {
                cell.detaileLab.textAlignment = NSTextAlignmentRight;
                cell.detaileLab.text = @"主人什么都没留下";
            }
        } else {
            cell.titleLab.text = @"设置备注";
            cell.detaileLab.text = self.friendInfo.remarkName.length > 0 ? self.friendInfo.remarkName : self.friendInfo.nikeName;
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }
        
        return cell;
    } else {
        NSArray *titleArr = @[@"消息置顶", @"消息免打扰"];
        TIMFriendOperationCell *cell = [tableView dequeueReusableCellWithIdentifier:[TIMFriendOperationCell description] forIndexPath:indexPath];
        cell.titleLab.text = titleArr[indexPath.row];
         TIMConversationInfo *conversation = [[TIMStorageManager sharedStorageManager] getSingleConversionByPartnerID:self.friendID];
        if (indexPath.row == 0) {
            cell.operationSwitch.on = [conversation.stickState isEqualToString:@"YES"] ? YES : NO;
            cell.operationBlock = ^(BOOL state) {
                [TIMApi setTopMessageBySessionId:self.friendID isTop:state];
            };
        } else if (indexPath.row == 1) {
            cell.operationSwitch.on = [self.friendInfo.disturb isEqualToString:@"NO"] ? YES : NO;
            cell.operationBlock = ^(BOOL state) {
                [TIMApi personalSetWithSessionId:self.friendID disturb:!state isGroup:0 completion:^(NSError *error) {
                    if (!error) {
                        NSLog(@"免打扰成功");
                    }
                }];
            };
        }
        return cell;
    }
}

#pragma mark - Private Method

- (void)deletedClicked {
    TIMUserInfo *user = [[TIMStorageManager sharedStorageManager] getCurrentUserInfo];
    [TIMApi addFriendWithSenderId:user.userID ReceiverId:self.friendID MsgMemo:self.friendInfo.remarkName MsgGroup:[@"fenzu" dataUsingEncoding:NSUTF8StringEncoding] MsgTip:[@"beizhu" dataUsingEncoding:NSUTF8StringEncoding] MsgOperation:TIMMsgOperation_OperationFriCancel completion:^(NSError *error) {
        if (!error) {
            NSLog(@"删除好友请求成功");
            dispatch_async(dispatch_get_main_queue(), ^{
                 [self.navigationController popToRootViewControllerAnimated:YES];
            });
        }
    }];
}

#pragma mark - Getter

- (UITableView *)friendInfoTab {
    if (!_friendInfoTab) {
        _friendInfoTab = [[UITableView alloc]initWithFrame:CGRectMake(0.0, 0.0, Screen_Width, Screen_Height-64.0) style:UITableViewStyleGrouped];
        [_friendInfoTab setDelegate:self];
        [_friendInfoTab setDataSource:self];
        [_friendInfoTab setSectionIndexBackgroundColor:[UIColor clearColor]];
        [_friendInfoTab setSectionIndexColor:[UIColor darkGrayColor]];
        [_friendInfoTab setBackgroundColor:RGBColor(240, 240, 240)];
        _friendInfoTab.separatorColor = RGBColor(240, 240, 240);
        [_friendInfoTab setSeparatorInset:UIEdgeInsetsMake(0, 15, 0, 0)];
        [_friendInfoTab setSeparatorInset:UIEdgeInsetsZero];
        [_friendInfoTab registerClass:[TIMFriendInfoCell class] forCellReuseIdentifier:[TIMFriendInfoCell description]];
        [_friendInfoTab registerClass:[TIMFriendOperationCell class] forCellReuseIdentifier:[TIMFriendOperationCell description]];
    }
    return _friendInfoTab;
}

- (TIMUserInfo *)friendInfo {
    if (!_friendInfo) {
        _friendInfo = [[TIMStorageManager sharedStorageManager] getFriendByFriendID:self.friendID];
    }
    return _friendInfo;
}
        
         
@end
